package com.lsy.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lsy.entity.AccountAndArticleCnd;
import com.lsy.entity.Article;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 项目名：springboot01
 * 包名：com.lsy.dao
 * 类名：ArticleMapper
 * 描述：ArticleMapper接口
 * 作者：柳胜酉
 * 开始时间：2023/6/11
 * 完成时间：
 * 版本：1.0
 */
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
    @Select("select article.article_id,article.article_title," +
            "article.article_content,article.type_name,article.upload_time," +
            "article.article_likes,article.state,account.user_id,account.account," +
            "account`password`,account.role from article,account " +
            "where article.user_id = account.user_id and article.state = 1 and article.user_id = #{user_id} order by article.upload_time desc;")
    List<Article> selectArticleByUserId(Integer user_id);

    @Select("select * from article where article_id = #{article_id};")
    Article selectArticleByArticleId(Integer article_id);

    @Delete("delete from article where article_id = #{article_id};")
    void deleteArticleByArticle_id(Integer article_id);

    @Update("update article set article_title = #{article_title},article_content = #{article_content},type_name = #{type_name}" +
            " where article_id = #{article_id};")
    void updateArticleByArticle_id(Article article);

    @Update("update article set article_likes = #{article_likes}" +
            " where article_id = #{article_id};")
    void updateArticleViews(AccountAndArticleCnd accountAndArticleCnd);

    @Update("update article set state = 1" +
            " where article_id = #{article_id};")
    void updateArticleToApproved(Article article);

    @Update("update article set state = 2" +
            " where article_id = #{article_id};")
    void updateArticleToFailed(Article article);

    @Select("select count(1) from article")
    Integer getArticleTotal();


}
